import Vue from 'vue'
import Vuex from 'vuex'
import http from '@/utils/http'

Vue.use(Vuex)
// store 只是存在内存中的
// store 配置持久化
export default new Vuex.Store({
  // 公共状态
  state: {
    cityName: '北京',
    cityId: '110100',
    cinemaList: []
  }, // 直接修改状态不可控
  // 集中式修改状态 - 被监控了！！！
  mutations: {
    changeCityName(state, name) {
      state.cityName = name
    },
    changeCityId(state, id) {
      state.cityId = id
    },
    setCinemaList(state, list) {
      state.cinemaList = list
    },
    clearCinemaList(state) { // 状态容易跟踪
      state.cinemaList = []
    }
  },
  actions: {
    getCinemaList({commit}, cityId) {
      return new Promise((resolve, reject) => {
        http({
          url: `/gateway?cityId=${cityId}&ticketFlag=1&k=4127869`,
          headers: {
            'X-Host': 'mall.film-ticket.cinema.list'
          }
        }).then((response) => {
          console.log(response)
          let res = response.data
          if (res.status === 0) {
            let data = res.data
            commit('setCinemaList', data.cinemas)
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  modules: {
  }
})
